<?php
if (!defined('BASEPATH'))
    exit('No direct script access allowed');


/**
 * Initialize Messages that will be shown on the role screen.
 * @access public 
 */
function init_role()
{
    $ci = &get_instance();
    
    // initialize role messages
    init_messages('Role');
  
}

/*
 * Method for validating posted values.
 * @access public
 * @retrun bool Returns true if the validation was successful, otherwise sends invalid response.
 */
function valid_add_role()
{
    $ci = &get_instance();
    $cms = $ci->cms;

    $ci->form_validation
            ->set_rules('name',        'name',         'trim|required|max_length[20]|xss_clean')
            ->set_rules('description', 'description',  'trim|max_length[1000]|xss_clean')
            ->set_rules('status',      'status',       'trim|max_length[10]|xss_clean');
            
    return $ci->form_validation->run();
}

/*
 * Method for validating posted values.
 * @access public
 * @retrun bool Returns true if the validation was successful, otherwise sends invalid response.
 */
function valid_edit_role()
{
    $ci = &get_instance();
    
    $ci->input->post('name')        ? $ci->form_validation->set_rules('name',           'name',         'trim|required|max_length[20]|xss_clean')   : '';
    $ci->input->post('description') ? $ci->form_validation->set_rules('description',    'description',  'trim|max_length[1000]|xss_clean')          : '';
    $ci->input->post('code')        ? $ci->form_validation->set_rules('code',           'code',         'trim|max_length[10]|xss_clean')            : '';
    
    $ci->form_validation
            ->set_message('is_unique', 'This country code is already existing');
    
    return $ci->form_validation->run();
}

/*
 * Method for validating form inputs from the role.
 * @access public
 * @retrun bool Returns true if the validation was successful, otherwise redirects to the role form.
 */
//function valid_role($action)
//{
//    $ci = &get_instance();
//
//    $ci->form_validation
//            ->set_rules('name', 'name', 'trim|required|max_length[20]|xss_clean')
//            ->set_rules('description', 'description', 'trim|required|max_length[1000]|xss_clean');
////          ->set_rules('status', 'status', 'trim|required|max_length[10]|xss_clean');
//    
//    if($action == 'edit')
//    {
//        $ci->form_validation->set_rules('id', 'ID', 'trim|required|numeric|max_length[10]|xss_clean');
//    }
//    
//    if ($ci->form_validation->run() == FALSE)
//    {
//        if($action == 'add')
//        {
//            view_role_form('Add Role', 'add_failed');
//        } else
//        {
//            $role = $ci->role_model->get_by_id( set_value('id') );
//            view_role_form('Edit Role', 'edit_failed', $role);
//        }
//    } else
//    {
//        return TRUE;
//    }
//}

/**
 * Displays roles list page.
 * @access public
 * @param array $content An array of roles to be shown.
 * @param string $message The type of message to be shown.
 */
function view_roles_list($content = NULL, $message_type = NULL)
{
    $ci = &get_instance();

    // create breadcrumb
    $ci->breadcrumb->clear();
    $ci->breadcrumb->add_crumb('Roles');
    
    $data = array();
    
    // set message
    $message                    = get_message($message_type);
    $data['message_type']       = $message['message_type'];
    $data['message']            = $message['message'];
    $data['message_title']      = $message['title'];
    $data['message_class']      = $message['class'];
    
    if($content === NULL)
    {
        $data['roles'] = $ci->role_model->get_all();
    } else
    {
        $data['roles'] = $content;
    }

    // active navigation
    $data['navigation'] = 'roles';

    // build breadcrumb
    $ci->breadcrumb->clear();
    $ci->breadcrumb->add_crumb('Roles');

    // build template
    $ci->template
            ->title('Roles')
            ->set_layout('main')
            ->set_partial('navigation', 'layouts/navigation')
            ->set_partial('footer', 'layouts/footer')
            ->set_partial('notifications', 'layouts/notifications')
//            ->set_partial('modal', 'layouts/modal')
            ->build('role_list', $data);
}

/**
 * Displays role info.
 * @access public
 * @param array $content An array of record info.
 */
function view_role_preview($content = NULL)
{
    $ci = &get_instance();

    // create breadcrumb
    $ci->breadcrumb->clear();
    $ci->breadcrumb->add_crumb('Preview Role');
    
    $data = array();

    if($content === NULL)
    {
        $data['roles'] = $ci->role_model->get_all();
    } else
    {
        $data['roles'] = $content;
    }

    // active navigation
    $data['navigation'] = 'roles';

    // build breadcrumb
    $ci->breadcrumb->clear();
    $ci->breadcrumb->add_crumb('Roles', base_url() . 'roles');
    $ci->breadcrumb->add_crumb('Role Preview');
    
    // build template
    $ci->template
            ->title('Role')
            ->set_layout('main')
            ->set_partial('navigation', 'layouts/navigation')
            ->set_partial('footer', 'layouts/footer')
//            ->set_partial('notifications', 'layouts/notifications')
//            ->set_partial('modal', 'layouts/modal')
            ->build('role_preview', $data);
}

/**
 * Displays the form for adding or editing roles.
 * @access public
 * @param string $title String representing the title page.
 * @param string $crumb The string that will be appended to the end of the breadcrumb.
 * @param string $message The type of message that will be shown on the form. Default value is null.
 * @param array $record An array containing record info to be edited. Default value is null.
 */
function view_role_form($title, $message_type = NULL, $record = NULL)
{
    $ci = &get_instance();
    $data = array();
    
    // set message
    $message                    = get_message($message_type);
    $data['message_type']       = $message['message_type'];
    $data['message']            = $message['message'];
    $data['message_title']      = $message['title'];
    $data['message_class']      = $message['class'];

    // set form data
    $data['action'] = $title == 'Add Role' ? 'add' : 'edit';
    $data['add'] = $title == 'Add Role' ? TRUE : FALSE;
    $data['add_another'] = $ci->input->post('add-another') ? ' checked="checked"' : '';
    
    // set modules
    $modules                        = $ci->module_model->get_all();
    $data['modules']                = $modules;
    
    // set permissions keys
    $permission_set = array('View', 'Add', 'Edit', 'Delete');
    foreach($modules as $module)
    {
        $permissions = isset($module['permissions']) ? unserialize($module['permissions']) : array();
        foreach($permissions as $permission)
        {
            $permission = changeCase($permission, 'human_upper');
            ( ! in_array($permission, $permission_set) ) ? array_push($permission_set, $permission) : '';
        }
    }
    $data['permission_set']         = $permission_set;
    
    // set role data
    $data['id']                     = isset($record['id']) ? $record['id'] : set_value('id');
    
    $data['name']                   = isset($record['name']) ? $record['name'] : set_value('name');
    $data['name-status']            = form_error('name') ? ' has-error' : '';
    
    $data['description']            = isset($record['description']) ? $record['description'] : set_value('description');
    $data['description-status']     = form_error('description') ? ' has-error' : '';
    
    $data['status']                 = isset($record['status']) ? $record['status'] : set_value('status');
    
    // set active navigation
    $data['navigation'] = 'roles';
    
    // create breadcrumb
    $ci->breadcrumb->clear();
    $ci->breadcrumb->add_crumb('Roles', base_url() . 'roles');
    $ci->breadcrumb->add_crumb($title);

    // build template
    $ci->template
            ->title($title)
            ->set_layout('main')
            ->set_partial('navigation', 'layouts/navigation')
            ->set_partial('footer', 'layouts/footer')
            ->set_partial('notifications', 'layouts/notifications')
//            ->set_partial('modal', 'layouts/modal')
            ->build('role_form', $data);
    
}

/**
 * Formats an array of records to valid response.
 * @access public
 * @param array $roles Array of roles
 */
function response_roles($roles)
{
    $ci = &get_instance();
    
    $params = $ci->input->post();
    $case = isset( $params['case'] ) ? $params['case'] : 'snake';
    
    $response_roles = array();
    foreach($roles as $role)
    {
        $response_role                   = array();
        isset($role['id'])               ? $response_role[changeCase('id', $case)]               = clean( $role['id'] ) : '';
        isset($role['name'])             ? $response_role[changeCase('name', $case)]             = clean( $role['name'] ) : '';
        isset($role['description'])      ? $response_role[changeCase('description', $case)]      = clean( $role['description'] ) : '';
        isset($role['permissions'])      ? $response_role[changeCase('permissions', $case)]      = clean( $role['permissions'] ) : '';
        isset($role['date_created'])     ? $response_role[changeCase('date_created', $case)]     = clean( $role['date_created'] ) : '';
        isset($role['date_modified'])    ? $response_role[changeCase('date_modified', $case)]    = clean( $role['date_modified'] ) : '';
        isset($role['status'])           ? $response_role[changeCase('status', $case)]           = clean( $role['status'] ) : '';

        array_push($response_roles, $response_role);
    }
    
    // we format our response
    if( count($response_roles) > 0 ) // check if not empty
    {
        format_response('success', 200, 'Response Roles.', NULL, $response_roles);
    }
    else
    {
        format_response('success', 200, 'No Data Available', NULL, 'No Data Available');
    }
    
}


/* End of file role_helper.php */
/* Location: ./application/roles/cms-roles/helpers/role_helper.php */